﻿using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using WaterCloud.Code;
using WaterCloud.Domain.ContractManagement;
using WaterCloud.Service;
using Microsoft.AspNetCore.Authorization;
using WaterCloud.Service.ContractManagement;
using WaterCloud.Service.SystemOrganize;
using WaterCloud.Service.SystemManage;
using iTextSharp.text;
using WaterCloud.Domain.SystemManage;

namespace WaterCloud.Web.Areas.ContractManagement.Controllers
{
    /// <summary>
    /// 创 建：超级管理员
    /// 日 期：2025-03-31 10:37
    /// 描 述：合同维护控制器类
    /// </summary>
    [Area("ContractManagement")]
    public class ContractMaintainController : BaseController
    {
        public ContractMaintainService _service { get; set; }
        public UserService _userservice { get; set; }
        public ItemsDataService _itemservice { get; set; }

        #region 获取数据
        [HttpGet]
        public virtual ActionResult Index2()
        {
            return View();
        }
        [HttpGet]
        public virtual ActionResult Waring1()
        {
            return View();
        }
        [HttpGet]
        public virtual ActionResult Waring2()
        {
            return View();
        }
        [HttpGet]
        public virtual ActionResult Waring3()
        {
            return View();
        }
        [HandlerAjaxOnly]
        [IgnoreAntiforgeryToken]
        public async Task<ActionResult> GetGridJson(SoulPage<ContractMaintainEntity> pagination, string keyword, bool b)
        {
            if (string.IsNullOrEmpty(pagination.field))
            {
                pagination.field = "F_CreatorTime";
                pagination.order = "desc";
            }
            var data = await _service.GetLookList(pagination, b, keyword);
            if (data.Count > 0)
            {
                for (int i = 0; i < data.Count; i++)
                {
                    data[i].F_Days = ((TimeSpan)(data[i].F_EndDate - DateTime.Now)).Days.ToString();
                }

                for (int i = 0; i < data.Count; i++)
                {
                    if (!string.IsNullOrEmpty(data[i].F_FullName))
                    {
                        var items =await _itemservice.GetItemList("ContractType");

                        foreach (var item in items)
                        {
                            if (item.F_ItemCode == data[i].F_FullName)
                            {
                                data[i].F_FullName = item.F_ItemName; 
                            }
                             
                        }
                      
                         }

                    }

                }
            
            return Content(pagination.setData(data).ToJson());
        }
        [HandlerAjaxOnly]
        [IgnoreAntiforgeryToken]
        public async Task<ActionResult> GetWaringJson1(SoulPage<ContractMaintainEntity> pagination, string keyword)
        {
            if (string.IsNullOrEmpty(pagination.field))
            {
                pagination.field = "F_CreatorTime";
                pagination.order = "desc";
            }
            var data = await _service.GetWaring1(pagination, keyword);
            if (data.Count > 0)
            {
                for (int i = 0; i < data.Count; i++)
                {
                    data[i].F_Days = ((TimeSpan)(data[i].F_EndDate - DateTime.Now)).Days.ToString();
                }

                for (int i = 0; i < data.Count; i++)
                {
                    if (!string.IsNullOrEmpty(data[i].F_FullName))
                    {
                        var items = await _itemservice.GetItemList("ContractType");

                        foreach (var item in items)
                        {
                            if (item.F_ItemCode == data[i].F_FullName)
                            {
                                data[i].F_FullName = item.F_ItemName;
                            }

                        }

                    }

                }

            }


            return Content(pagination.setData(data).ToJson());
        }
        [HandlerAjaxOnly]
        [IgnoreAntiforgeryToken]
        public async Task<ActionResult> GetWaringJson2(SoulPage<ContractMaintainEntity> pagination, string keyword)
        {
            if (string.IsNullOrEmpty(pagination.field))
            {
                pagination.field = "F_CreatorTime";
                pagination.order = "desc";
            }
            var data = await _service.GetWaring2(pagination, keyword);
           if (data.Count > 0)
            {
                for (int i = 0; i < data.Count; i++)
                {
                    data[i].F_Days = ((TimeSpan)(data[i].F_EndDate - DateTime.Now)).Days.ToString();
                }

                for (int i = 0; i < data.Count; i++)
                {
                    if (!string.IsNullOrEmpty(data[i].F_FullName))
                    {
                        var items =await _itemservice.GetItemList("ContractType");

                        foreach (var item in items)
                        {
                            if (item.F_ItemCode == data[i].F_FullName)
                            {
                                data[i].F_FullName = item.F_ItemName; 
                            }
                             
                        }
                      
                         }

                    }

                }

            return Content(pagination.setData(data).ToJson());
        }



        [HttpGet]
        [HandlerAjaxOnly]
        public async Task<ActionResult> GetListJson(string keyword)
        {
            var data = await _service.GetList(keyword);
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public async Task<ActionResult> GetFormJson(string keyValue)
        {
            var data = await _service.GetLookForm(keyValue);
            return Content(data.ToJson());
        }
        #endregion

        #region 提交数据
        [HttpPost]
        [HandlerAjaxOnly]
        public async Task<ActionResult> SubmitForm(ContractMaintainEntity entity, string keyValue)
        {
            try
            {
                await _service.SubmitForm(entity, keyValue);
                return await Success("操作成功。", "", keyValue);
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", keyValue);
            }
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        public async Task<ActionResult> CloseForm(string keyValue)
        {
            try
            {
                await _service.CloseForm(keyValue);
                return await Success("操作成功。", "", keyValue, DbLogType.Delete);
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", keyValue, DbLogType.Delete);
            }
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        public async Task<ActionResult> DeleteForm(string keyValue)
        {
            try
            {
                await _service.DeleteForm(keyValue);
                return await Success("操作成功。", "", keyValue, DbLogType.Delete);
            }
            catch (Exception ex)
            {
                return await Error(ex.Message, "", keyValue, DbLogType.Delete);
            }
        }
        #endregion
    }
}
